/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Golf_Manager.model;

import Golf_Manager.vo.JoinMember;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Administrator
 */
public class GroupMemberModel extends AbstractTableModel {
    
    Class[] types = new Class [] {java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class };
    private List<Golf_Manager.vo.JoinMember> joinMembers = new ArrayList<>();
    private String[] columns = new String [] {"조이름", "이름", "생년월일", "점수"};
    private boolean[] canEdit = new boolean [] { false, false, false, true };

    public GroupMemberModel() {
    }
    
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return types[columnIndex];
    }
    
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return canEdit[columnIndex];
    }
    
    @Override
    public int getRowCount() {
        return joinMembers.size();
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }
    
    @Override
    public String getColumnName(int column) {
        return columns[column];
    }
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch(columnIndex){
            case 0: {
//                System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " getName : " + joinMembers.get(rowIndex).getName());
                return joinMembers.get(rowIndex).getGroupNm();
            }
            case 1: {
//                System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " getName : " + joinMembers.get(rowIndex).getName());
                return joinMembers.get(rowIndex).getName();
            }
            case 2: {
//                System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " getName : " + joinMembers.get(rowIndex).getName());
                return joinMembers.get(rowIndex).getBirth();
            }
            case 3: {
//                System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " getBirth : " + joinMembers.get(rowIndex).getBirth());
                return joinMembers.get(rowIndex).getScore();
            }
            default: {
//                System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " default : " + null);
                return null;
            }
        }
    }
    
    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
//        System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " aValue : " + aValue);
        switch(columnIndex){
            case 3: {
                if(aValue != null){
                    joinMembers.get(rowIndex).setScore(aValue.toString());
                }
            }
        }
        fireTableDataChanged();
    }
    
    public List<Golf_Manager.vo.JoinMember> getJoinMembers(){
        return this.joinMembers;
    }
    
    public void setJoinMembers(List<JoinMember> joinMembers) {
        this.joinMembers = new ArrayList<>(joinMembers);
        fireTableDataChanged();
    }
    
    public JoinMember getJoinMember(int rowIndex){
        return this.joinMembers.get(rowIndex);
    }
    
    public void addJoinMember(JoinMember joinMember){
        this.joinMembers.add(joinMember);
        fireTableDataChanged();
    }
    
    public void delJoinMember(int rowIndex){
        this.joinMembers.remove(rowIndex);
        fireTableDataChanged();
    }
}
